package cn.lixin.product.service.impl;

import cn.lixin.basic.service.impl.BaseServiceImpl;
import cn.lixin.product.domain.Product;
import cn.lixin.product.domain.ProductDetail;
import cn.lixin.product.mapper.ProductDetailMapper;
import cn.lixin.product.mapper.ProductMapper;
import cn.lixin.product.service.IProductService;
import cn.lixin.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public void save(Product product,Long shop_id) {
        product.setShop_id(shop_id);
        productMapper.save(product);
        ProductDetail productDetail = product.getDetail();
        if (productDetail != null){
            productDetail.setProduct_id(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("onSaleTime",new Date());
        productMapper.onSale(map);
    }
    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        productMapper.offSale(map);
    }

    @Override
    public Product queryById(Long id) {
        System.out.println(id);
        return productMapper.loadById(id);
    }

    /**
     * 购买 ==> : 1下架商品：state=0、更新下架时间
     *           2绑定用户：setUserId
     *
     * @param productId
     * @param logininfoId
     */
    @Override
    public void buy(Long productId, Long logininfoId) {
        Product product = productMapper.loadById(productId);
        /* 不能下架product.setState(0);*/
/*        Integer count = product.getSalecount(); // 先获取之前的销量，再加上本次购买的数量
        Integer salecount = count + number;// 再加上本次购买的数量
        product.setSalecount(salecount);*/
        product.setOffsaletime(new Date());
//        User user = userMapper.loadByLogininfoId(logininfoId);
//        product.setUser_id(user.getId());
        productMapper.update(product);

    }

}
